﻿using ClosedXML.Excel;
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Http;

using SanJ.FreeCare.CodeSystems;
using SanJ.FreeCare.ConditionDefinitions;
using SanJ.FreeCare.Locations;
using SanJ.FreeCare.ObservationDefinitionItems;
using SanJ.FreeCare.ObservationDefinitions;
using SanJ.FreeCare.Peis.Configure.Disease;
using SanJ.FreeCare.Peis.Dto;
using SanJ.FreeCare.ValueSets;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.BlobStoring;
using Volo.Abp.ObjectMapping;

namespace SanJ.FreeCare.Peis
{
    public partial class PeisAppService : FreeCareAppService, IPeisAppService
    {
        private readonly IBlobContainer<ExcelTemplateContainer> _blobContainer;
        private readonly ObservationDefinitionManager _observationDefinitionManager;
        private readonly ObservationDefinitionItemManager _observationDefinitionItemManager;
        private readonly LocationManager _locationManager;
        private readonly ConditionDefinitionManager _conditionDefinitionManager;
       // private readonly AppletManager _appletManager;
        public PeisAppService(IBlobContainer<ExcelTemplateContainer> blobContainer,
            ObservationDefinitionManager observationDefinitionManager,
            ObservationDefinitionItemManager observationDefinitionItemManager,
            ConditionDefinitionManager conditionDefinitionManager,
            LocationManager locationManager
           // AppletManager appletManager
            )
        {
            _blobContainer = blobContainer;
            _locationManager = locationManager;
            _observationDefinitionManager = observationDefinitionManager;
            _observationDefinitionItemManager = observationDefinitionItemManager;
            _conditionDefinitionManager = conditionDefinitionManager;
           // _appletManager = appletManager;
        }

        public async Task<CodeSystemDto> CreateCodeSystemAsync(CreateCodeSystemInput input)
        {
            var codeSystem = ObjectMapper.Map<CreateCodeSystemInput, CodeSystem>(input);
            var insertedEntity = await _codeSystemManager.CreateAsync(codeSystem);
            return ObjectMapper.Map<CodeSystem, CodeSystemDto>(insertedEntity);
        }
        public async Task<CodeSystemDto> UpdateCodeSystemAsync(UpdateCodeSystemInput input)
        {
            var codeSystem = ObjectMapper.Map<UpdateCodeSystemInput, CodeSystem>(input);
            var updatedEntity = await _codeSystemManager.UpdateAsync(codeSystem);
            return ObjectMapper.Map<CodeSystem, CodeSystemDto>(updatedEntity);
        }
        public async Task DeleteCodeSystemAsync(Guid id)
        {
            await _codeSystemManager.DeleteAsync(id);
        }

        public async Task<ListResultDto<CodeConcept>> GetCodeListAsync(string? systemName)
        {
            List<CodeConcept> result = new List<CodeConcept>();

            if (systemName == "DetailDataType" || systemName == "detaildatatype")
            {
                result =
                [
                    new CodeConcept() { Code = "double", Display = "数字" },
                    new CodeConcept() { Code = "string", Display = "字符" },
                    new CodeConcept() { Code = "date", Display = "日期" },
                ];
            }

            if (systemName.IsNullOrEmpty())
            {
                result = await _codeSystemManager.GetAllAsync();
            }

            return new ListResultDto<CodeConcept>()
            {
                Items = result
            };
        }

        public async Task ImportExcelAsync(IFormFile excel, string templateName)
        {
            if (excel == null) throw new ArgumentNullException(nameof(excel));
            List<ObservationDefinition> odList = new List<ObservationDefinition>();

            using (var workbook = new XLWorkbook(excel.OpenReadStream()))
            {
                var worksheet = workbook.Worksheet(templateName);
                if (worksheet == null) throw new UserFriendlyException("上传模板无效");
                var count = 0;
                foreach (var row in worksheet.Rows())
                {
                    count += 1;
                    if (count > 1) //skip the first row.
                    {
                        ObservationDefinition od = new ObservationDefinition(
                           // Guid.Empty,
                           //row.Cell(1).Value.ToString(),
                           //row.Cell(2).Value.ToString(),
                           //row.Cell(3).Value.ToString(),
                           //row.Cell(4).Value.ToString(),
                           //row.Cell(5).Value.ToString(),
                           //row.Cell(6).Value.ToString()
                            );
                        odList.Add( od );
                    }
                }
            }

            if (odList.Count > 0)
            {
                await _observationDefinitionManager.CreateManyAsync(odList);
            }

        }

        //public Task<ListResultDto<DiseaseDto>> GetDiseaseAsync(GetDiseaseInput input)
        //{
        //    throw new NotImplementedException();
        //}

        //Task<ListResultDto<DiseaseDto>> IPeisAppService.GetDiseaseListAsync(GetDiseaseInput input)
        //{
        //    throw new NotImplementedException();
        //}
    }
}
